package com.lgs.demo.base;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.lgs.demo.R;
import com.lgs.demo.utils.LogUtil;

public class BaseActivity extends AppCompatActivity {

    protected Fragment mCurrentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    /**
     * @param from 当前显示的Fragment, 即将被隐藏
     * @param to   将要切换到的Fragment，即将要显示
     */
    protected void changeFragment2(Fragment from, Fragment to) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (from == to || to == null) {
            return;
        }
        mCurrentFragment = to;
        if (null != from) {
            ft.hide(from);
        }
        if (to.isAdded()) {
            ft.show(to).commit();
        } else {
            ft.add(R.id.fmContainer, to).commit();

        }
    }

    /**
     * @param from 当前显示的Fragment, 即将被隐藏
     * @param to   将要切换到的Fragment，即将要显示
     */
    protected void changeFragment(Fragment from, Fragment to) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        if (from != to) {
            mCurrentFragment = to;

            //to没有被添加的情况
            if (!to.isAdded()) {

                if (null != from) {
                    //隐藏from
                    ft.hide(from);
                }
                if (null != to) {
                    //添加to
                    ft.add(R.id.fmContainer, to).commit();
                }
                //to已被添加的情况
            } else {
                if (null != from) {
                    ft.hide(from);
                }
                if (null != to) {
                    //显示to
                    ft.show(to).commit();
                }
            }
        }
    }
	
	public final void pushFragment(int containerViewId, Fragment fragment, boolean isReplace) {
        String str = fragment.getClass().getName();
        FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
        localFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        localFragmentTransaction.setCustomAnimations(R.anim.back_enter, R.anim.back_exit);
        if (isReplace) {
            //替换掉当前显示的Fragment
            localFragmentTransaction.replace(containerViewId, fragment, str);
        } else {
            //添加新的Fragment并显示
            localFragmentTransaction.add(containerViewId, fragment, str);
        }
        localFragmentTransaction.addToBackStack(str);
        localFragmentTransaction.commit();
    }

    public final void pushFragment(Fragment fromFragment, Fragment toFragment, int containerViewId) {
        LogUtil.i("下一个Fragment");
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        //        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        int count = fm.getBackStackEntryCount();
        LogUtil.i("返回栈中fragment个数：" + count);
        //如果这个Fragment没有通过tag找到，说明此Fragment不存在，所以这里重新添加一个，在这时就指定了它的tag了
        //        Fragment fragment = fm.findFragmentByTag(tag);
        if (null == fromFragment || null == toFragment) {
            return;
        }
        if (fromFragment != toFragment) {
            // 判断切换的fragment是否已经添加过
            if (!toFragment.isAdded()) {
                // 如果没有添加，先把当前的fragment隐藏，把切换的fragment加上并显示
                ft.hide(fromFragment).add(containerViewId, toFragment);
            } else {
                // 如果已经添加过，则先把当前的fragment隐藏，把切换的fragment显示
                ft.hide(fromFragment).show(toFragment);
            }
        }
        ft.show(toFragment);
        ft.addToBackStack(toFragment.getClass().getName());
        LogUtil.i("返回栈中fragment个数：" + count);
        ft.commit();
       /* //当前fragment被添加并可见
        if (fromFragment.isAdded() && fromFragment.isVisible()) {
            //隐藏当前fragment
            ft.hide(fromFragment);
        }
        //如果要显示的Fragment没有添加，
        if (!toFragment.isAdded()) {
            ft.add(containerViewId, toFragment);
        }
        //显示
        ft.show(toFragment);
        ft.addToBackStack(null);
        ft.commit();*/
    }

}
